package org.feng.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.feng.condition.Condition;
import org.feng.base.entity.Result;
import org.feng.annotation.AccessLog;
import org.feng.entity.PubResource;
import org.feng.support.UploadSupport;
import org.springframework.web.bind.annotation.*;
import org.feng.service.IPubResourceService;
import org.feng.base.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import jakarta.annotation.Resource;

import java.util.List;
import java.util.Arrays;

/**
 * 资源表
 *
 * @author : FengShuai
 * @since : 2024/6/27 9:24
 */
@Tag(name = "资源表")
@RestController
@RequestMapping("/system/sysResource")
public class SysResourceController extends BaseController {

    @Resource
    private IPubResourceService sysResourceService;
    @Resource
    private UploadSupport uploadSupport;

    /**
     * 开关资源
     *
     * @param pubResource 更新体
     * @return 返回体
     */
    @PostMapping("/switch")
    @AccessLog("开关资源")
    @Operation(summary = "开关资源")
    public Result<Boolean> switchStart(@RequestBody PubResource pubResource) {
        sysResourceService.update(Wrappers.<PubResource>lambdaUpdate().set(PubResource::getStatus, false));
        sysResourceService.update(Wrappers.<PubResource>lambdaUpdate()
                .eq(PubResource::getId, pubResource.getId())
                .set(PubResource::getStatus, pubResource.getStatus()));
        uploadSupport.updateUploadImpl();
        return Result.status(true);
    }

    /**
     * 分页列表
     *
     * @param pubResource 查询体
     * @return 返回体
     */
    @GetMapping("/pageList")
    @Operation(summary = "分页列表")
    public Result<IPage<PubResource>> pageList(PubResource pubResource) {
        return Result.success(sysResourceService.page(Condition.getPage(), Wrappers.lambdaQuery(pubResource)));
    }

    /**
     * 列表
     *
     * @param pubResource 查询体
     * @return 返回体
     */
    @GetMapping("/list")
    @Operation(summary = "列表")
    public Result<List<PubResource>> list(PubResource pubResource) {
        return Result.success(sysResourceService.list(Wrappers.lambdaQuery(pubResource)));
    }

    /**
     * 保存
     *
     * @param pubResource 保存体
     * @return 返回体
     */
    @PostMapping("/save")
    @AccessLog("保存资源表")
    @Operation(summary = "保存")
    public Result<Boolean> save(@RequestBody PubResource pubResource) {
        if (pubResource.getStatus()) {
            sysResourceService.update(Wrappers.<PubResource>lambdaUpdate().set(PubResource::getStatus, false));

        }
        sysResourceService.save(pubResource);
        uploadSupport.updateUploadImpl();
        return Result.status(true);
    }

    /**
     * 更新
     *
     * @param pubResource 更新体
     * @return 返回体
     */
    @PostMapping("/update")
    @AccessLog("更新资源表")
    @Operation(summary = "更新")
    public Result<Boolean> update(@RequestBody PubResource pubResource) {
        if (pubResource.getStatus()) {
            sysResourceService.update(Wrappers.<PubResource>lambdaUpdate().set(PubResource::getStatus, false));
        }
        sysResourceService.updateById(pubResource);
        uploadSupport.updateUploadImpl();
        return Result.status(true);
    }

    /**
     * 删除
     *
     * @param ids 逗号分割的id
     * @return 返回体
     */
    @GetMapping("/remove")
    @AccessLog("删除资源表")
    @Operation(summary = "删除")
    public Result<Boolean> remove(@RequestParam("ids") String ids) {
        return Result.status(sysResourceService.removeBatchByIds(Arrays.asList(ids.split(","))));
    }
}
